﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;
using System.Threading;
using TaxiDispatchClient.Service;

namespace TaxiDispatchClient
{
    public partial class GmapOverlayAddEditForm : DevExpress.XtraEditors.XtraForm
    {
        public GmapOverlayAddEditForm()
        {
            InitializeComponent();
        }
        //添加
        public GmapOverlayAddEditForm(double lng,double lat)
        {
            InitializeComponent();
            textEdit_long.Text = lng.ToString();
            textEdit_lat.Text = lat.ToString();

          
            imgCombo_star.SelectedIndex = 2;
            imgCombo_type.SelectedIndex = 0;
        }
        //更改
        public GmapOverlayAddEditForm(int id)
        {
            InitializeComponent();
            _id = id;
            try
            {
                DataRow[] rows = MainForm.DtOverlayList.Select("id=" + id);
                if(rows.Length<0)
                {
                    XtraMessageBox.Show("未找到该标记，请核对！");
                    Close();
                }             
                textEdit_lat.Text = rows[0]["Latitude"].ToString();
                textEdit_long.Text = rows[0]["Longitude"].ToString();
                textEdit_name.Text = rows[0]["OverlayName"].ToString();
                memoEdit_remark.Text = rows[0]["Remark"].ToString();
               
                imgCombo_type.SelectedItem = imgCombo_type.Properties.Items.GetItem(rows[0]["OverlayType"].ToString());
                imgCombo_star.SelectedItem = imgCombo_star.Properties.Items.GetItem(rows[0]["Star"].ToString());
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
                Close();
            }                       
           
        }

        private readonly int _id;

        private bool AddMarker()
        {
           
            try
            {
                GmapoverlayInfo marker = GetNewMarker();
                if (MainForm.PositionService.GmapoverlayAdd(marker))
                {
                    Thread t = new Thread(RefreshDt){IsBackground = true};
                    t.Start();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private void RefreshDt()
        {
            try
            {
               string strWhere = "";
               if (MainForm.MyUserInfo.CompanyName != PowerConfig.MainCompanyName)
               {
                   strWhere = "City = " + MainForm.MyUserInfo.CompanyId;
               }
               MainForm.DtOverlayList = MainForm.PositionService.GmapoverlayGetList(strWhere).Tables[0];
            }
            catch (Exception ex)
            {
                Common.CommonRoutine.HandlingExceptions(ex);
            }
        }

        private GmapoverlayInfo GetNewMarker()
        {
            string name = textEdit_name.Text;
            string lat = textEdit_lat.Text;
            string lng = textEdit_long.Text;
            string remark = memoEdit_remark.Text;
            string city = MainForm.MyUserInfo.CompanyId.ToString();
            string type = imgCombo_type.Properties.Items[imgCombo_type.SelectedIndex].Value.ToString();
            string star = null;
            if (imgCombo_star.SelectedIndex != -1)
                star = imgCombo_star.Properties.Items[imgCombo_star.SelectedIndex].Value.ToString();
            GmapoverlayInfo marker = new GmapoverlayInfo()
            {
                Name = name,
                City = city,
                Latitude = double.Parse(lat),
                Longitude = double.Parse(lng),
                Type = type,
                Remark = remark,
                CompanyId = MainForm.MyUserInfo.CompanyId
            };
            if (star != null) marker.Star = int.Parse(star);
            return marker;
        }
        private bool UpdateMarker()
        {
            if(_id==0) return false;
            GmapoverlayInfo marker = GetNewMarker();
            marker.id = _id;
            try
            {
                if (MainForm.PositionService.GmapoverlayUpdate(marker))
                {
                    DataRow[] rows = MainForm.DtOverlayList.Select("id=" + _id);
                    if(rows.Length>0)
                    {
                        rows[0]["OverlayName"] = marker.Name;
                        rows[0]["Overlaytype"] = marker.Type;
                        rows[0]["City"] = marker.City;
                        rows[0]["Latitude"] = marker.Latitude;
                        rows[0]["Longitude"] = marker.Longitude;
                        rows[0]["Star"] = marker.Star;
                        rows[0]["Remark"] = marker.Remark;                        
                    }
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if(CheckValue())
            {
                if(_id==0)
                {
                    if (AddMarker())
                    {
                        XtraMessageBox.Show("标记添加成功！");
                        this.DialogResult = DialogResult.OK;
                        Close();
                    }
                    else
                    {
                        XtraMessageBox.Show("添加失败，请重试！");                        
                    }
                }
                else
                {
                    if (UpdateMarker())
                    {                        
                        XtraMessageBox.Show("更新成功！");
                        this.DialogResult = DialogResult.OK;
                        Close();
                    }
                    else
                    {
                        XtraMessageBox.Show("更新失败，请重试");                        
                    }
                }
            }
        }


        private bool CheckValue()
        {
            if(textEdit_name.Text.Trim()=="")
            {
                XtraMessageBox.Show("标记名称不能为空！");
                textEdit_name.Focus();
                return false;
            }
            if(textEdit_lat.Text.Trim()=="")
            {
                XtraMessageBox.Show("维度不能为空！");
                textEdit_lat.Focus();
                return false;
            }
            if(textEdit_long.Text.Trim()=="")
            {
                XtraMessageBox.Show("经度不能为空！");
                textEdit_long.Focus();
                return false;
            }
            double lat = double.Parse(textEdit_lat.Text.Trim());
            double lng = double.Parse(textEdit_long.Text.Trim());
            if ( lat<-90 ||lat>90)
            {
                XtraMessageBox.Show("请保证纬度为正确取值范围内！");
                textEdit_lat.Focus();
                return false;
            }
            if (lng<-180||lng>180)
            {
                XtraMessageBox.Show("请保证经度为正确取值范围内！");
                textEdit_long.Focus();
                return false;
            }
           
            if (imgCombo_type.SelectedIndex==-1)
            {
                XtraMessageBox.Show("请选择所属类型");
                imgCombo_type.Focus();
                return false;
            }
            return true;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


      #region 窗体控件效果
        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;
            Cursor = Cursors.Arrow;
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Close();
        }


        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void Form_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
#endregion

    }
}